import matter from 'gray-matter';
import { removeLeadingSlash, optionallyAddLeadingSlash } from '../fs/index.js';
import { getOpenApiTitleAndDescription } from '../openapi/getOpenApiTitleAndDescription.js';
import { pagePathToSlug } from './pagePathToSlug.js';
import { slugToTitle } from './slugToTitle.js';
export const getPageMetadataAndSlug = (pagePath, pageContent, openApiFiles) => {
    const { data: metadata } = matter(pageContent);
    const slug = pagePathToSlug(pagePath);
    const defaultTitle = slugToTitle(slug);
    // Append data from OpenAPI if it exists
    let openApiTitle;
    let openApiDescription;
    if (metadata.openapi) {
        const { title, description } = getOpenApiTitleAndDescription(openApiFiles, metadata.openapi);
        openApiTitle = title;
        openApiDescription = description;
    }
    const pageMetadata = Object.assign(Object.assign({ title: openApiTitle !== null && openApiTitle !== void 0 ? openApiTitle : defaultTitle, description: openApiDescription }, metadata), { href: optionallyAddLeadingSlash(slug) });
    return {
        pageMetadata,
        slug: removeLeadingSlash(slug),
    };
};
